package kr.hkit.androidcomplete02.ch01;

import java.util.ArrayList;
import java.util.zip.Inflater;

import kr.hkit.androidcomplete02.R;

import android.app.Activity;
import android.content.Context;
import android.net.MailTo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CustomAdapterView extends Activity {
	ArrayList<MyItem> arLists;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_view_test);

		arLists = new ArrayList<>();
		arLists.add(new MyItem(R.drawable.ic_launcher, "삼성 노트북"));
		arLists.add(new MyItem(R.drawable.btnexit, "LG 세탁기"));
		arLists.add(new MyItem(R.drawable.toycar, "대우 마티즈"));
		
		MyListAdapter adapter = new MyListAdapter();
		
		ListView listView = (ListView) findViewById(R.id.listView);
	}

	class MyListAdapter extends BaseAdapter {
		LayoutInflater inflate;
		String TAG = "MyListAdapter";
		public MyListAdapter() {
			super();
			inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			Log.d(TAG,"getCont()");
			return arLists.size();
		}

		@Override
		public Object getItem(int position) {
			Log.d(TAG,"getItem("+ position + ")");
			return arLists.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflate.inflate(R.layout.icontext, parent, false);
			}
			ImageView img = (ImageView) convertView.findViewById(R.id.img);
			img.setImageResource(arLists.get(position).mIcon);
			
			TextView tv = (TextView) convertView.findViewById(R.id.text);
			tv.setText(arLists.get(position).mName);
			
			Button btn = (Button) convertView.findViewById(R.id.btn);
			btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					String text = arLists.get(position).mName + "�� �ֹ��մϴ�.";
					Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
				}
			});
			return null;
		}

	}

	class MyItem {
		int mIcon;
		String mName;

		public MyItem(int mIcon, String mName) {
			super();
			this.mIcon = mIcon;
			this.mName = mName;
		}

	}
}
